package com.dacool.fragment;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dacool.app.sale.R;
import com.dacool.http.BaseRequest;
import com.dacool.http.BaseRequest.ResponseHandler;
import com.dacool.http.GetRecommendsRequest;
import com.dacool.model.DCRecommend;
import com.dacool.model.DCUser;
import com.dacool.util.ViewUtils;
import com.dacool.view.TitleItem;
import com.dacool.view.pulltorefesh.PullToRefreshListView;

public class RecommenderListFragment extends BaseFragment {

	private ListView listView;
	private ArrayList<DCRecommend> recommends = new ArrayList<DCRecommend>();
	private ArrayAdapter<DCRecommend> mAdapter;

	@Override
	protected int myLayoutID() {
		// TODO Auto-generated method stub
		return R.layout.frag_recommendlist;
	}

	@Override
	protected View myView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		listView = ViewUtils.findViewById(view, R.id.listView);
		if (mAdapter == null)
			mAdapter = new ArrayAdapter<DCRecommend>(getActivity(), 0) {
				public View getView(int position, View convertView,
						ViewGroup parent) {
					if (convertView == null) {
						View view = LayoutInflater.from(getContext()).inflate(
								R.layout.recommenderlistitem, null);
						ViewHolder holder = new ViewHolder();
						holder.name = ViewUtils.findViewById(view, R.id.name);
						holder.phone = ViewUtils.findViewById(view, R.id.phone);
						holder.status = ViewUtils.findViewById(view,
								R.id.status);
						convertView = view;
						convertView.setTag(holder);
					}
					ViewHolder holder = (ViewHolder) convertView.getTag();
					DCRecommend recommend = recommends.get(position);
					holder.name.setText(recommend.name);
					holder.phone.setText(recommend.phone);
					holder.status.setText(recommend.status);
					holder.status.setTextColor(DCRecommend.colorList[1]);
					if ("验证中".equals(recommend.status)) {
						holder.status.setTextColor(DCRecommend.colorList[0]);
					}

					convertView
							.setBackgroundColor(position % 2 == 0 ? 0xffEEEEEE
									: 0xffffffff);
					return convertView;
				};

				public int getCount() {
					return recommends.size();
				};

				class ViewHolder {
					public TextView name;
					public TextView phone;
					public TextView status;
				}
			};
		listView.setAdapter(mAdapter);
	}

	@Override
	public TitleItem getRightItem() {
		if (mRightItem == null) {
			Button rightButton = new Button(getActivity());
			rightButton.setText("我要推荐");
			rightButton.setTextColor(Color.WHITE);
			rightButton
					.setBackgroundResource(R.drawable.button_green_white_border);
			rightButton.setTextSize(15);
			rightButton.setPadding(0, 0, 0, 0);
			mRightItem = new TitleItem();
			mRightItem.view = rightButton;
			mRightItem.height = ViewUtils.rp(30);
			mRightItem.width = ViewUtils.rp(80);
			rightButton.setOnClickListener(this);
		}
		return mRightItem;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == mRightItem.view) {
			AddRecommenderFragment fragment = new AddRecommenderFragment();
			getContainerActivity().addFragment(fragment);
		}
		super.onClick(v);
	}

	@Override
	protected boolean showDefaultSecondLeftButton() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public String titleText() {
		// TODO Auto-generated method stub
		return "我的推荐";
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		this.showProgressDialog(null, false);
		new GetRecommendsRequest().setMemberId(DCUser.sharedUser().getUserId())
				.setHandler(new ResponseHandler() {

					/**
					 * [{ “name":”张三”,”time”:”2014-4-8 16:21:20”,status:”验证中”},
					 * { “name ":” 张三”,”time”:”2014-4-8 16:21:20”,
					 * status:”推荐成功”}, { “name ":” 张三”,”time”:”2014-4-8
					 * 16:21:20”,coolMoney:” 验证”}]
					 */
					@Override
					public void handleResponse(BaseRequest request,
							int statusCode, String errorMsg, String response) {
						RecommenderListFragment.this.dismissProgressDialog();
						if (statusCode != 200)
							return;
						try {
							JSONObject object = new JSONObject(response);
							JSONArray array = object.getJSONArray("data");
							final ArrayList<DCRecommend> newrecommends = new ArrayList<DCRecommend>();
							for (int i = 0; i < array.length(); i++) {

								JSONObject obj = array.getJSONObject(i);
//								Log.d("test", "recommend" + obj);
								DCRecommend recommend = new DCRecommend();
								recommend.name = obj.getString("toName");
								int status = obj.optInt("isReg", 0);
								boolean expired = obj.optBoolean("expired", false);
								recommend.status = expired?"已超期":status == 0 ? "验证中" : "验证通过";
								recommend.phone = obj.getString("toMobile");
								newrecommends.add(recommend);
							}
							ViewUtils.runInMainThread(new Runnable() {

								@Override
								public void run() {
									// TODO Auto-generated method stub
									recommends = newrecommends;
									mAdapter.notifyDataSetChanged();
								}
							});
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}).start();
	}
}
